package com.apci.ipreda.client.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.User;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractFormController;

import com.apci.ipreda.client.web.form.DeclaracionForm;
import com.apci.ipreda.domain.Declaracion;
import com.apci.ipreda.domain.Entidad;
import com.apci.ipreda.domain.Periodo;
import com.apci.ipreda.security.domain.Usuario;
import com.apci.ipreda.service.EntidadService;
import com.apci.ipreda.service.TipoDataBaseService;
import com.apci.ipreda.service.UsuarioService;

public class DeclaracionController extends AbstractFormController {
	
	private TipoDataBaseService tipoDataBaseService;
	private UsuarioService usuarioService;
	private EntidadService entidadService;

	public void setUsuarioService(UsuarioService usuarioService) {
		this.usuarioService = usuarioService;
	}

	
	protected ModelAndView processFormSubmission(
			HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse, Object paramObject,
			BindException paramBindException) throws Exception {
		
		
		/*Map<String, Object> model = new HashMap<String, Object>();
		model.put("declaracionForm", declaracionForm);
		*/
		logger.info("processFormSubmission");
		
		return showForm(paramHttpServletRequest, paramBindException, "secure/declaracionList");	
	}

	
	protected ModelAndView showForm(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse,
			BindException paramBindException) throws Exception {
		// TODO Auto-generated method stub
		return showForm(paramHttpServletRequest, paramBindException, "secure/declaracionList"); 
	}
	
	
	
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		// TODO Auto-generated method stub
		logger.info("----------------referenceData---------------");
		Map<String,Object> referenceData = new HashMap<String,Object>();
		SecurityContext securityCtx = SecurityContextHolder.getContext();
		
		if(securityCtx.getAuthentication()!=null){
			User user = (User)securityCtx.getAuthentication().getPrincipal();
			String userName = user.getUsername();
			this.logger.info("userName->" + userName);
			Usuario usuario = this.usuarioService.getUsuarioByUsername(userName);
			
			Entidad entidad = null;
			List<Periodo> periodoList = tipoDataBaseService.getPeriodoList();
			if(usuario.getEntidad()!=null && usuario.getEntidad().getId()!=null){
				entidad = usuario.getEntidad();
				request.getSession().setAttribute("entidadId", entidad.getId());
			}
			
			DeclaracionForm declaracionForm = (DeclaracionForm) formBackingObject(request);
			declaracionForm.getDeclaracion().setEntidad(entidad);
			String periodo = null;
			if(!CollectionUtils.isEmpty(entidad.getDeclaracionList())){
				List<Declaracion> declaracionList = entidad.getDeclaracionList();
				for (Declaracion declaracion : declaracionList) {
					periodo = String.valueOf(declaracion.getPeriodo());
					for (int i = 0 ; i < periodoList.size() ;i++) {
						Periodo per = periodoList.get(i);
						 if (per.getDescripcion().equalsIgnoreCase(periodo)) {
							periodoList.remove(per);
						}
					}
				}
			}
			declaracionForm.setPeriodoList(periodoList);
			
			referenceData.put("declaracionForm", declaracionForm);
		}
		return referenceData;
		
	}

	public void setEntidadService(EntidadService entidadService) {
		this.entidadService = entidadService;
	}

	public void setTipoDataBaseService(TipoDataBaseService tipoDataBaseService) {
		this.tipoDataBaseService = tipoDataBaseService;
	}
	
}
